package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;

import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
@Slf4j
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    //查询指定分类下菜品
    @GetMapping("/dish/list")
    public ResultInfo findList(Long categoryId, Integer status) {

        //1.获取dish_13246543132键
        String key = "dish_"+categoryId;

             ValueOperations vps = redisTemplate.opsForValue();
             List<Dish> dishList = (List<Dish>) vps.get(key);
             if(CollectionUtil.isNotEmpty(dishList)){
                 log.info("从redis中查询");
                 return ResultInfo.success(dishList);
             }else {
                 log.info("c从数据库中获取");
                 dishList = dishService.findList(categoryId, null);
                 //存到redis中
                 vps.set(key,dishList);
                 return ResultInfo.success(dishList);
             }
    }
}

